Skip to content

Conversation

usbalbin
Copy link

@usbalbin usbalbin commented Oct 6, 2025

This PR adds the concept of "local tasks". Local tasks are tasks that may only be spawned from other tasks with the same priority, thus running on the same executor. Thanks to this, there is no requirement on the arguments to local tasks to be Send/Sync. Local tasks will therefore not have the usual my_task::spawn method available.

All tasks, local or not, that may spawn other local tasks will have a public field local_spawner in their Context. This type will then have methods for all local tasks that may be spawned. Tasks are made into local tasks by setting the task attribute is_local_task = true (default false).

#[app(device = pac, peripherals = false, dispatchers = [SPI1])]
mod app {
    use super::*;

    #[shared]
    struct Shared {}

    #[local]
    struct Local {}

    #[init]
    fn init(_cx: init::Context) -> (Shared, Local) {
        task1::spawn().ok();
        //task2::spawn(Default::default()).ok(); <--- This is rejected since it is a local task
        (Shared {}, Local {})
    }

    #[task(priority = 1)]
    async fn task1(cx: task1::Context) {
        defmt::info!("Hello from task1!");

        // This is the only way to spawn task2, by using the local spawner
        cx.local_spawner.task2(Default::default()).ok();
    }

    //    Note the same prio as above
    //     vvvvvvvv
    #[task(priority = 1, is_local_task = true)] // <-- This is a local task
    async fn task2(_cx: task2::Context, _nsns: super::NotSendNotSync) {
        defmt::info!("Hello from task1!");
    }
}

#[derive(Default)]
struct NotSendNotSync(PhantomData<*mut u8>);

@usbalbin usbalbin changed the title Try to add spawn-local WIP - Try to add spawn-local Oct 6, 2025
@usbalbin usbalbin changed the title WIP - Try to add spawn-local Add local tasks Oct 7, 2025
@usbalbin
Copy link
Author

usbalbin commented Oct 7, 2025

CI seems quite angry, however I am not so sure if it is all my fault...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant